package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dk.mapper.MaterialBomMapper;
import com.dk.model.Material;
import com.dk.mapper.MaterialMapper;
import com.dk.model.MaterialBom;
import com.dk.service.MaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.vo.MaterialVO;
import org.aspectj.lang.annotation.AdviceName;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-17
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Resource
    private MaterialMapper materialMapper;
    @Resource
    private MaterialBomMapper materialBomMapper;

    @Override
    public void insertInfoMaterial(MaterialVO materialVO) {
        Material material = new Material();
        BeanUtils.copyProperties(materialVO,material);
        materialMapper.insert(material);
    }

    @Override
    public MaterialVO getInfoMaterialById(String mid) {
        //查询物料信息
        Material material = materialMapper.selectById(mid);
        MaterialVO materialVO = new MaterialVO();
        BeanUtils.copyProperties(material,materialVO);
        //查询bom信息,通过mid
        LambdaQueryWrapper<MaterialBom> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaterialBom::getMaterialId,mid);
        List<MaterialBom> materialBoms = materialBomMapper.selectList(wrapper);
        if (materialBoms.size()==0){
            return materialVO;
        }
        //获取bomId
        List<String> bomids = new ArrayList<>();
        for (MaterialBom materialBom : materialBoms) {
            String bomId = materialBom.getBomId();
            bomids.add(bomId);
        }
        //通过bomids去查询 物料信息
        List<MaterialVO> materialVOS = materialMapper.selectBatchIds(bomids).stream().map(material1 -> {
            MaterialVO vo = new MaterialVO();
            BeanUtils.copyProperties(material1, vo);
            return vo;
        }).collect(Collectors.toList());
        materialVO.setList(materialVOS);
        return materialVO;
    }

    @Override
    public List<Material> getInfoMaterialByIds(List<String> materialIds) {
        List<Material> materialList = materialMapper.selectBatchIds(materialIds);
        return materialList;
    }
}
